package com.suray.wcs.service.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.commin.core.utils.StringUtils;
import com.suray.wcs.service.enums.PortModel;
import com.suray.wcs.service.system.entity.PlcDB;
import com.suray.wcs.service.system.entity.PortDB;
import com.suray.wcs.service.system.mapper.PortMapper;
import com.suray.wcs.service.system.service.IPortDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PortDBServiceImpl extends ServiceImpl<PortMapper, PortDB> implements IPortDBService {
    @Autowired
    private PortMapper portMapper;

    //分页查询
    @Override
    public List<PortDB> selectList(PortDB portDB) {
        QueryWrapper<PortDB> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(portDB.getPortName())) {
            queryWrapper.lambda().like(PortDB::getPortName, portDB.getPortName());
        }
        if (StringUtils.isNotEmpty(portDB.getPortCode())) {
            queryWrapper.lambda().like(PortDB::getPortCode, portDB.getPortCode());
        }
        if (portDB.getPortMode() != null) {
            queryWrapper.lambda().like(PortDB::getPortMode, portDB.getPortMode());
        }
        if (portDB.getStatus() != null) {
            queryWrapper.lambda().eq(PortDB::getStatus, portDB.getStatus());
        }
        return portMapper.selectList(queryWrapper);

    }

    @Override
    public int updatePortStatus(PortDB portDB) {
        return portMapper.updateById(portDB);
    }

    /**
     * 批量删除
     * @param portIds 主键数组
     * @return
     */
    @Override
    public int deletePortByIds(Long[] portIds) {
        List<Long> portIdList = new ArrayList();
        for (Long portId : portIds) {
            portIdList.add(portId);
        }
        return portMapper.deleteBatchIds(portIdList);
    }

    /**
     * 根据模式获取库口
     * @param portModel
     * @return
     */
    @Override
    public List<PortDB> getByModel(Integer portModel) {
        LambdaQueryWrapper<PortDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PortDB::getPortMode, portModel).or().eq(PortDB::getPortMode, PortModel.IN_AND_OUT.getValue());
        return portMapper.selectList(queryWrapper);
    }

    /**
     * 通过库口编码查询
     *
     * @param portCode 和WMS保持一致
     * @return
     */
    public PortDB selectByPortCode(String portCode) {
        LambdaQueryWrapper<PortDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PortDB::getPortCode, portCode);
        return portMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean checkPortCodeUnique(PortDB portDB) {
        Long portId = StringUtils.isNull(portDB.getPortId()) ? -1L : portDB.getPortId();
        PortDB info = this.selectByPortCode(portDB.getPortCode());
        if (StringUtils.isNotNull(info) && info.getPortId().longValue() != portId.longValue()) {
            return true;
        }
        return false;
    }

    public PortDB selectByPortName(String portName) {
        LambdaQueryWrapper<PortDB> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PortDB::getPortName, portName);
        return portMapper.selectOne(queryWrapper);
    }
}
